﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace nRoute.Behaviors.Interactivity
{
    public static class Interaction
    {
        
#region Dependency Properties

        private static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached(
            "Behaviors",
            typeof(AttachableCollection<Behavior>),
            typeof(Interaction),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnSetBehaviorsCallback));

        public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached(
            "Triggers",
            typeof(AttachableCollection<TriggerBase>),
            typeof(Interaction),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnSetTriggersCallback));

#endregion

#region Getters and Setters

        public static void SetBehaviors(FrameworkElement dependencyObject, AttachableCollection<Behavior> behaviors)
        {
            dependencyObject.SetValue(BehaviorsProperty, behaviors);
        }

        public static AttachableCollection<Behavior> GetBehaviors(FrameworkElement dependencyObject)
        {
            return GetOrCreateBehaviors(dependencyObject);
        }

        public static void SetTriggers(FrameworkElement dependencyObject, AttachableCollection<TriggerBase> triggers)
        {
            dependencyObject.SetValue(TriggersProperty, triggers);
        }

        public static AttachableCollection<TriggerBase> GetTriggers(FrameworkElement dependencyObject)
        {
            return GetOrCreateTriggers(dependencyObject);
        }

#endregion

#region Handlers

        private static void OnSetBehaviorsCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _targetObject = dependencyObject as FrameworkElement;
            if (_targetObject != null)
            {
                AttachableCollection<Behavior> _behaviours = GetOrCreateBehaviors(_targetObject);
                ((IAttachedObject)_behaviours).Attach(_targetObject);
            }
        }

        private static void OnSetTriggersCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _targetObject = dependencyObject as FrameworkElement;
            if (_targetObject != null)
            {
                AttachableCollection<TriggerBase> _triggers = GetOrCreateTriggers(_targetObject);
                ((IAttachedObject)_triggers).Attach(_targetObject);
            }
        }

#endregion

#region Helper

        private static AttachableCollection<Behavior> GetOrCreateBehaviors(FrameworkElement dependencyObject)
        {
            var _behaviours = dependencyObject.GetValue(BehaviorsProperty) as AttachableCollection<Behavior>;
            if (_behaviours == null)
            {
                _behaviours = new AttachableCollection<Behavior>();
                dependencyObject.SetValue(BehaviorsProperty, _behaviours);
            }

            return _behaviours;
        }

        private static AttachableCollection<TriggerBase> GetOrCreateTriggers(FrameworkElement dependencyObject)
        {
            var _triggers = dependencyObject.GetValue(TriggersProperty) as AttachableCollection<TriggerBase>;
            if (_triggers == null)
            {
                _triggers = new AttachableCollection<TriggerBase>();
                dependencyObject.SetValue(TriggersProperty, _triggers);
            }

            return _triggers;
        }

#endregion

    }
}
